<template>
  <div>
    <div class="cate-wrap">
      <div class="cate-left">
        <div class="cates">
          <div
          v-for="(cate, index) in cates"
          :key="cate.id"
          @click="swithTab(index)"
          :class="['cate', {
            active: index === num
          }] ">{{cate.name}}</div>
        </div>
      </div>
      <div class="cate-right">
        <div v-if="!items.length">
          <img src="@/assets/01.png" style="width:264px;height:300px" alt="">
        </div>
         <div v-else class="items">
        <div class="item" @click="$router.push(`/Detail/${item.id}`)" v-for="item in items" :key="item.id">
          <img :src="item.pic" alt="" class="item-left">
          <div class="item-info">
            <h3 class="name">{{item.name}}</h3>
            <p class="info">
              {{ item.characteristic ? item.characteristic :'暂无描述' }}
            </p>
            <div class="item-action">
              <p class="price">
                {{item.minPrice | currency('￥')}}
              </p>
              <van-icon name="add-o" color="red" size="24"></van-icon>
            </div>
          </div>
          </div>
        </div>
      </div>
    </div>
    <tabbar/>
  </div>
</template>

<script>
import Tabbar from '../../components/Tabbar.vue'
import { fetchCates, fetchItems } from '@api'
export default {
  data () {
    return {
      cates: [],
      items: [],
      num: 0
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    swithTab (index) {
      // 点击切换分类
      this.num = index
      this.fetchItems()
    },
    fetchCates () { // 请求所有分类
      fetchCates().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItems()
        }
      })
    },
    fetchItems () { // 请求商品的数据
      this.items = []
      fetchItems({ // 拿到分类商品的id
        categoryId: this.cates[this.num].id
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
        } else {
          this.items = []
        }
      })
    }
  },
  filters: {
    filterInfo (v, len = 10) {
      if (v.length > 10) {
        return v.substr(0, 10) + '...'
      } else {
        return v
      }
    },
    currency (v, sign = '￥') {
      return sign + v
    }
  },
  components: {
    Tabbar
  }
}
</script>

<style lang="scss" scoped>
.cate-wrap{
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 50px;
  display: flex;
  .cate-left{
    height: 100%;
    width: 110px;
    border-right:1px solid rgb(204, 203, 203);
    background-color: rgb(247, 246, 246);
    overflow-y: auto;
    .cate{
      text-align: center;
      line-height: 40px;
      font-size: 14px;
      color: #333;
      &.active{
        border-left: 3px solid red;
        background-color: #fff;
      }

    }
  }
  .cate-right{
    height: 100%;
    flex: 1;
    overflow-y:auto;
    .items{
      .item{
        display: flex;
        &-left{
          padding-left: 10px;
          width: 110px;
          height: 110px;
        }
        &-info{
          flex: 1;
          box-sizing: border-box;
          text-align: center;
          padding-top: 10px;
          color: #666;
          font-size: 16px;
          .name{
            font-size: 16px;
            color: #333;
            line-height: 32px;
          }
          .info{
            margin:15px 0;
            font-size: 12px;
            color: #666;
          }
          .item-action{
            display: flex;
            margin-top: 30px;
            justify-content: space-around;
            .price{
              color: rgb(190, 21, 21);
              font-size: 18px;
            }
          }
        }
      }
    }
      }
}
</style>
